/**
 * 自定义验证
 */


$.extend(
		$.fn.validatebox.defaults.rules,
		{
			//验证poolname的组成，长度，唯一性
			poolname : {
				validator : function(value){
					if(/^[a-zA-Z_][\w]{3,23}$/.test(value)){
							var poolName = value;
							$.ajax({
								type : 'get',
								url: baseURL + "/cluster/pool/validate",
								data: {poolName:poolName},
								async:false,
								beforeSend: function() {
								},
								success:function(json){
									if(json.result && json.result == "success"){
								  		$.fn.validatebox.defaults.rules.poolname.message = localeMsg.verification.correct;	
								  	}else{
								  		$.fn.validatebox.defaults.rules.poolname.message = localeMsg.verification.repeat;
								  	}
								}
							});
							if($.fn.validatebox.defaults.rules.poolname.message == localeMsg.verification.correct){
								return true;
							}else{
								return false;
							}
					}else{
						$.fn.validatebox.defaults.rules.poolname.message = localeMsg.verification.poolnamelimit;	
						return false;
					}
				},
			},
			
			// 验证输入框输入大小
			number : {
				validator : function(value, param) {
					if ( value < param[0]
							|| value > param[1]) {
						$.fn.validatebox.defaults.rules.number.message = '请输入'
								+ param[0] + '到' + param[1] + '范围的数';
						return false;
					} 
					else {
						$.fn.validatebox.defaults.rules.number.message = localeMsg.verification.correct;
						return true;
					}
				},
			},
			// 验证输入框输入大小,大于某个值
			numbermore : {
				validator : function(value, param) {
					if ( value < param[0]) {
						$.fn.validatebox.defaults.rules.numbermore.message = '请输入大于'
								+ param[0] + '的整数' ;
						return false;
					} 
					else {
						$.fn.validatebox.defaults.rules.numbermore.message = localeMsg.verification.correct;
						return true;
					}
				},
			},
			// 验证输入框输入大小,小于某个值
			numberless : {
				validator : function(value, param) {
					if ( value > param[0]) {
						/*$.fn.validatebox.defaults.rules.numbermore.message = '请输入小于'
								+ param[0] + '的整数' ;*/
						$.fn.validatebox.defaults.rules.numbermore.message = '请输入小于的整数' ;
						return false;
					} 
					else {
						$.fn.validatebox.defaults.rules.numbermore.message = localeMsg.verification.correct;
						return true;
					}
				},
			},
			//正整数
			positive_integer :{
				validator : function(value){
					if (!/^\+?[1-9][0-9]*$/.test(value)) {
						$.fn.validatebox.defaults.rules.positive_integer.message = localeMsg.verification.positiveinteger;
						return false;
					} 
					else {
						$.fn.validatebox.defaults.rules.positive_integer.message = localeMsg.verification.correct;
						return true;
					}
				},
			},
			//2的正整次方(2^0<='x'<=2^32)
			pgnum : {
				validator : function(value){
					var aa = value;
					if(/^10{0,32}$/.test((value).toString(2))){
						$.fn.validatebox.defaults.rules.pgnum.message = localeMsg.verification.pgcount;
						return false;
					}
					else{
						$.fn.validatebox.defaults.rules.pgnum.message = localeMsg.verification.correct;
						return true;
					}
				},
			},
			//非负数
			positive_number : {
				validator : function(value){
					var quotamax = value;
					var poolName = $('#poolname').val();
					if(!/^[0-9].*$/.test(value)){
						$.fn.validatebox.defaults.rules.positive_number.message = localeMsg.verification.positivenumber;
						return false;
					}
					else{
						$.ajax({
							type:'get',
							url:baseURL + "/cluster/pool/valid",
							data:{quotamax:quotamax,poolName:poolName},
							async:false,
							beforeSend: function() {
							},
							success:function(json){
								if(json.result && json.result == "success"){
							  		$.fn.validatebox.defaults.rules.positive_number.message = localeMsg.verification.correct;	
							  	}else{
							  		$.fn.validatebox.defaults.rules.positive_number.message = localeMsg.verification.quotomax+json.max;
							  	}
							}
						});
						if($.fn.validatebox.defaults.rules.positive_number.message == localeMsg.verification.correct){
							return true;
						}else{
							return false;
						}
					}
				},
			},
			//非负数
			digits : {
				validator : function(value){
					if(!/^\+?(0|[1-9][0-9]*)$/.test(value)){
						$.fn.validatebox.defaults.rules.digits.message = localeMsg.verification.digits;
						return false;
					}
					else{
						$.fn.validatebox.defaults.rules.digits.message = localeMsg.verification.correct;
						return true;
					}
				},
			},
			//imagename的组成，长度，唯一性
			imagename : {
				validator : function(value){
					if(!/^[a-zA-Z_][\w]{3,24}$/.test(value)){
						$.fn.validatebox.defaults.rules.imagename.message = localeMsg.verification.poolnamelimit;	
						return false;
					}else{
						var information;
						var name = value;
						var poolname = $("#poolName").val();
						$.ajax({
							type : 'get',
							url : baseURL + "/cluster/lun/validate",
							data: {name:name,poolname:poolname},
							async: false,
							beforeSend: function() {
							},
							success:function(json){
								if(json.result && json.result == "success"){
							  		$.fn.validatebox.defaults.rules.imagename.message = localeMsg.verification.correct;	
							  		return true;
							  	}else{
							  		$.fn.validatebox.defaults.rules.imagename.message = localeMsg.verification.imagename;
							  		return false;
							  	}
							}
						});
						if($.fn.validatebox.defaults.rules.imagename.message == localeMsg.verification.correct){
							return true;
						}else{
							return false;
						}
					}
					},
				message:localeMsg.verification.imagename
			},
			//iopsLength长度
			iopsLength : {
				validator : function(value){
					var quotamax = value;
					var information;
					var value = localeMsg.lun.iopsScope;
					$.ajax({
						type:'get',
						url: baseURL + "/cluster/lun/validateIops",
						//禁止异步
						async: false,
						data : {quotamax:quotamax},
						beforeSend: function() {
						},
						success :function(json){
							if(json.result && json.result == "success"){
								information = true;
							}else{
								$.fn.validatebox.defaults.rules.iopsLength.message ="iops的范围必须在11-1000000000之间";
								information = false;
							}
						}
					});
					return information;
				},
				message:localeMsg.verification.max
			},
			//bandwidth长度
			bandwidthLength : {
				validator : function(value){
					var max;
					var quotamax = value;
					var information;
					$.ajax({
						type:'get',
						url: baseURL + "/cluster/lun/validateBandwidth",
						//禁止异步
						async: false,
						data : {quotamax:quotamax},
						beforeSend: function() {
						},
						success :function(json){
							if(json.result && json.result == "success"){
								information = true;
							}else{
								information = false;
								$.fn.validatebox.defaults.rules.bandwidthLength.message ="带宽的范围必须在1000-1000000000之间"
							}
						}
					});
					return information;
				},
				message:localeMsg.verification.max
			},
			
			//ip验证
			ip : {
				validator : function(value){
					 if(/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/.test(value)){
						 $.fn.validatebox.defaults.rules.ip.message = localeMsg.verification.correct;	
						 return true;
					 }else{
						 $.fn.validatebox.defaults.rules.ip.message = localeMsg.verification.ip;
						 return false;
					 }
				},
			},
			//验证name，可填空格
			name : {
				validator : function(value){
					if(/^[a-zA-Z_\u3E00-\u9FA5][\s\w\u3E00-\u9FA5]{1,23}$/.test(value)){
						$.fn.validatebox.defaults.rules.name.message = localeMsg.verification.correct;
						return true;
					}else{
						$.fn.validatebox.defaults.rules.name.message = localeMsg.verification.namelimit;	
						return false;
					}
				},
			},
			//验证value
			value : {
				validator : function(value){
					var type =$("#type").val();
					if(type=="3"){
						if(/^[0-9].*$/.test(value)){
							$.fn.validatebox.defaults.rules.value.message = localeMsg.verification.correct;
							return true;
						}else{
							$.fn.validatebox.defaults.rules.value.message = localeMsg.verification.firvalue;	
							return false;
						}
					}else if(type=="6"){
						if(/^([1]?\d{1,3})$/.test(value)){
							$.fn.validatebox.defaults.rules.value.message = localeMsg.verification.correct;
							return true;
						}else{
							$.fn.validatebox.defaults.rules.value.message = localeMsg.verification.dlayvalue;	
							return false;
						}
					}
					else{
						/*/^(0|[1-9]\d|100)$/
*/						if(/^([1]?\d{1,2})$/.test(value)){  
							$.fn.validatebox.defaults.rules.value.message = localeMsg.verification.correct;
							return true;
						}else{
							$.fn.validatebox.defaults.rules.value.message = localeMsg.verification.secvalue;	
							return false;
						}
					}
				},
			},
			size : {
				validator : function(value){
					var information;
					var poolname =$("#poolName").val();
					var size = value;
					if(!/^[0-9].*$/.test(value)){
						$.fn.validatebox.defaults.rules.size.message = localeMsg.verification.positivenumber;
						return false;
					}else{
						$.ajax({
							type : 'get',
							url: baseURL + "/cluster/lun/validateSize",
							async: false,
							data : {size:size,poolname:poolname},
							beforeSend: function() {
							},
							success :function(json){
								if(json.result && json.result == "success"){
							  		$.fn.validatebox.defaults.rules.size.message = localeMsg.verification.correct;	
							  	}else{
							  		$.fn.validatebox.defaults.rules.size.message = localeMsg.verification.size+json.maxavail+localeMsg.verification.or+"(128TB)";
							  	}
							}
						});
						if($.fn.validatebox.defaults.rules.size.message == localeMsg.verification.correct){
							return true;
						}else{
							return false;
						}
					}
				}
			},
			
			uniqueip : {
				validator : function(value,param){
						var information;
						var ip = value;
						//var url = param;
						var url = baseURL + param[0];
						var id = param[1];
						$.ajax({
							type: 'get',
							url: url,
							//禁止异步
							async: false,
							data : {ip : ip , id : id},
							beforeSend: function() {
							},
							success :function(json){
								if(json.result && json.result == "success"){
									information = true;
								}
							}
						});
						return information;
				},
				message: localeMsg.verification.name
			},
	/*unique*/uniquename : {
				validator : function(value,param){
						var information;
						var name = value;
						//var url = param;
						var url = baseURL + param[0];
						var id = param[1];
						$.ajax({
							type: 'get',
							url: url,
							//禁止异步
							async: false,
							data : {name:name,id : id},
							beforeSend: function() {
							},
							success :function(json){
								if(json.result && json.result == "success"){
									information = true;
								}
							}
						});
						return information;
				},
				message: localeMsg.verification.name
			},
			//验证name
			vername : {
				validator : function(value){
					if(/^[a-zA-Z_][\w]{3,24}$/.test(value)){
						$.fn.validatebox.defaults.rules.vername.message = localeMsg.verification.correct;
						return true;
					}else{
						$.fn.validatebox.defaults.rules.vername.message = localeMsg.verification.poolnamelimit;	
						return false;
					}
				},
			},
		});
		